import { Image as VanImage } from "vant";
import { scrollMixin } from "utils/mixin";
export default {
  name: "",
  //import引入的组件需要注入到对象中才能使用
  components: {
    VanImage
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      isCurrent: true,
      currentText: "查看上期", //本期还是上期
      active: 0,
      page: 1,
      isLoadMore: true,
      total_page: 0,
      rankTabs: [],
      rankList: [],
      moneyInfo: {},
      userSelf: {},
      time: "",
      id: "",
      listApi: ""
    };
  },
  //监听属性 类似于data概念
  computed: {
    jackpotClass() {
      return this.isCurrent ? "red" : "";
    }
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    rankTabOn(id) {
      //切换tabs
      this.id = id;
      this.page = 1;
      this.getRankList();
    },
    examineOn() {
      //切换当前还是上一期
      this.isCurrent ? (this.currentText = "返回") : (this.currentText = "查看上期");
      this.isCurrent = !this.isCurrent;
      this.active = 0;
      this.rankList = [];
      this.id = "";
      this.rankIndex();
    },
    bonusOn() {
      //小红花明细
      this.$router.push(this.fun.getUrl("bonusDetail", {}, {}));
    },
    rankIndex() {
      let api = "";
      //会员信息
      if (this.isCurrent) {
        api = "plugin.ranking-dividend.frontend.controllers.rank.index";
        this.listApi = "plugin.ranking-dividend.frontend.controllers.rank.rank";
      } else {
        api = "plugin.ranking-dividend.frontend.controllers.rank.last";
        this.listApi = "plugin.ranking-dividend.frontend.controllers.rank.last-rank";
      }
      $http
        .post(api, {}, "loading")
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            this.time = data.time;
            this.rankTabs = data.levels;
            if (data?.levels.length > 0) {
              this.id = data?.levels[0]?.id;
            }
            if (this.id !== "") {
              this.getRankList();
            }
          } else {
            this.$dialog.alert({
              message: res.msg
            });
          }
        })
        .catch(err => {
          console.log("getUserData::err", err);
        });
    },
    getRankList() {
      let query = {
        page: this.page,
        level_id: this.id
      };
      $http.post(this.listApi, query, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          let rankList = [];
          rankList = res.list?.data;
          this.isLoadMore = true;
          this.total_page = res.list.last_page;
          if (!this.total_page) {
            this.page = 0;
          }
          this.rankList = rankList || [];
          this.moneyInfo = res.info; //分销商金额
          this.userSelf = res.self; //分销商排名
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let query = {
          page: this.page,
          level_id: this.id
        };
        $http.post(this.listApi, query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let data = res.data;
              let rankList = data.list.data;
              this.rankList = [...this.rankList, ...rankList]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.rankIndex();
  }
};
